package com.javasm.mingming.common.utils;

import com.alibaba.excel.EasyExcel;
import com.javasm.mingming.test.entity.TestStudent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author javasm
 * @version 0.1
 * @className ExportUtil
 * @descriptioin:
 * @date 2025/1/18 11:45
 * @since jdk11
 */
@Component
public class ExportUtil<T> {
    @Resource
    HttpServletResponse response;

    public void expExcel(String fileName, List<T> list,Class<T> tClass){
        try {
            //设置响应的头信息
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            //文件名
            fileName = URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+","%20");
            response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
            //使用easyExcel 写入数据
            EasyExcel.write(response.getOutputStream(), tClass).sheet("数据1").doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void expWord(String fileName, String templateName, Map<String,Object> dataModel){
        try {
            //设置头信息
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setCharacterEncoding("UTF-8");
            fileName = URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+","%20");
            response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".docx");
            //配置 freemarker
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
            //默认utf-8
            configuration.setDefaultEncoding("UTF-8");
            //设置模板异常处理 ,当产生异常的时候 向上抛
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            //禁止在日志中输入 模板异常
            configuration.setLogTemplateExceptions(false);
            //开启自动检查异常,
            configuration.setWrapUncheckedExceptions(true);
            //禁止是循环变量为空的时候 使用默认值
            configuration.setFallbackOnNullLoopVariable(false);
            //文件夹路径
            configuration.setClassForTemplateLoading(this.getClass(),"/docx");
            //加载模板
            Template template = configuration.getTemplate(templateName);
            //输出
            OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream(),"UTF-8");
            //写出数据
            template.process(dataModel,writer);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }
    }

}
